<?php

class UserController extends Controller
{
    public function actionIndex()
    {
        $criteria = new CDbCriteria();
        $criteria->addCondition('deleted = 0','AND');
        $count = SysUser::model()->count($criteria);

        $pager = new CPagination($count);
        $pager->pageSize = 10;

        $pager->applyLimit($criteria);

        $users = SysUser::model()->findAll($criteria);
        $this->data['pages'] = $pager;
        $this->data['users'] = $users;

        $this->render('index',$this->data);
    }

    public function actionConfig()
    {
        $this->data['message'] = null;
        $this->render('config');
    }

    public function actionSaveconfig(){
        $oldpassword = $_REQUEST['oldpassword'];
        $password = $_REQUEST['password'];

        $user_id = Yii::app()->user->user_id;
        $userinfo = $this->connection->createCommand("select * from sys_user where user_id = :user_id")->bindParam(':user_id', $user_id, PDO::PARAM_INT)
            ->queryRow();

        if($userinfo['password']==$oldpassword){
            $this->connection->createCommand("update sys_user set password = :password where user_id = :user_id")
                ->bindParam(':user_id', $user_id, PDO::PARAM_INT)
                ->bindParam(':password', $password, PDO::PARAM_STR)
                ->execute();
            $this->data['message'] = Appcode::success;
            $this->render('config',$this->data);
        }else{
            $this->data['message'] = Appcode::error_oldpassworderror;
            $this->render('config',$this->data);
        }

    }

    public function actionGetdata(){
//
//        $criteria = new CDbCriteria();
//        $criteria->addCondition('deleted = 0','AND');
//        $count = Article::model()->count($criteria);
//
//        $pager = new CPagination($count);
//        $pager->pageSize = 10;
//
//        $pager->applyLimit($criteria);
//
//        $users = User::model()->findAll($criteria);
//        $this->data['pages'] = $pager;
//
//        echo json_encode($data);
    }

    public function actionShowedit(){

        $user_id = $_REQUEST['user_id'];
        $roles = $this->connection->createCommand("select * from sys_authitem where type = 2 and deleted = 0")->queryAll();
        $this->data['roles'] = $roles;

        $user = null;
        if($user_id==0){
            //新增
            $this->data['user'] = $user;
            $this->render('showadd',$this->data);
        }else{
            //编辑
            $user = $this->connection->createCommand("select * from sys_user where user_id = :user_id")->bindParam(':user_id',$user_id,PDO::PARAM_INT)->queryRow();
            $this->data['user'] = $user;

            $userroles = $this->connection->createCommand("select itemname from sys_authassignment where userid = :userid")
                ->bindParam(':userid',$user['username'],PDO::PARAM_STR)
                ->queryAll();
            $userrolesarray = array();
            foreach ($userroles as $ur){
                $userrolesarray[$ur['itemname']] = $ur['itemname'];
            }
            $this->data['userrolesarray'] = $userrolesarray;

            $this->render('showedit',$this->data);
        }
    }

    public function actionResetpassword(){
        $user_id = $_REQUEST['user_id'];
        $user = $this->connection->createCommand("select * from sys_user where user_id = :user_id")->bindParam(':user_id',$user_id,PDO::PARAM_INT)->queryRow();
        $this->data['user'] = $user;
        $this->render('resetpassword',$this->data);
    }

    public function actionDoreset(){
        $user_id = $_REQUEST['user_id'];
        $password = $_REQUEST['password'];
        $this->connection->createCommand("update sys_user set password = :password where user_id = :user_id")
            ->bindParam(':password',$password,PDO::PARAM_STR)
            ->bindParam(':user_id',$user_id,PDO::PARAM_STR)
            ->execute();

        $this->data['message'] = Appcode::success;
        $this->actionIndex();
    }

    public function actionSavedata(){

        $user_id = $_REQUEST['user_id'];
        $username = $_REQUEST['username'];
        $realname = $_REQUEST['realname'];

        if($user_id!=null){

        }else{
            //新增
            $this->connection->createCommand("insert into sys_user (username,realname) values (:username,:realname)")
                ->bindParam(':username',$username,PDO::PARAM_STR)
                ->bindParam(':realname',$realname,PDO::PARAM_STR)
                ->execute();
        }

        //角色进行更新
        $roles = $_REQUEST['roles'];

        //清理权限
        $this->connection->createCommand("delete from sys_authassignment where userid = :userid")->bindParam(':userid',$username,PDO::PARAM_STR)->execute();

        //权限赋权限
        $auth = Yii::app()->authManager;
        foreach($roles as $r){
            $auth->assign($r,$username);
        }

        $this->data['message'] = Appcode::success;
        $this->actionIndex();

    }

    public function actionRemove(){
        $user_id = $_REQUEST['user_id'];
        $this->connection->createCommand("update sys_user set deleted = 1 
where user_id = :user_id")
            ->bindParam(':user_id',$user_id,PDO::PARAM_INT)
            ->execute();

        $this->data['message'] = Appcode::success;
        $this->actionIndex();

    }

    /**
     * 加载用户的树形结构，全量
     */
    public function actionLoadusertree(){
        $result = $this->connection->createCommand("SELECT USER_ID AS id,realname as text FROM crm_user;")->queryAll();
        echo json_encode($result);
    }
}